using System;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace SpiderMaster.AspNet.TradingSystem.UI
{
    public partial class MyProfile_aspx : TradingCenterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FirstNameTextBox.Text = Server.HtmlDecode(TradingCenterProfileCommon.Current.FirstName);
                LastNameTextBox.Text = Server.HtmlDecode(TradingCenterProfileCommon.Current.LastName);
                EmailTextBox.Text = Membership.GetUser().Email;
            }
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            EmailNotValid.Visible = false;
            Page.Validate("ChangeProfile");
            if (Page.IsValid)
            {
                TradingCenterProfileCommon.Current.FirstName = Server.HtmlEncode(FirstNameTextBox.Text);
                TradingCenterProfileCommon.Current.LastName = Server.HtmlEncode(LastNameTextBox.Text);

                MembershipUser u = Membership.GetUser();
                u.Email = Server.HtmlEncode(EmailTextBox.Text);
                
                try
                {
                    Membership.UpdateUser(u);
                    BackToMyAds();
                }
                catch
                {
                    EmailNotValid.Text = "This Email is in use or not allowed.";
                    EmailNotValid.Visible = true;
                }
                            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            BackToMyAds();
        }

        protected void BackToMyAds()
        {
            Response.Redirect("~/MyAds.aspx");
        }
        protected void ChangePasswordControl_ChangedPassword(object sender, EventArgs e)
        {
            Page.SetFocus(ChangePasswordControl);
        }
        protected void FirstNameValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;
          
            if (Regex.IsMatch(FirstNameTextBox.Text, @"^[0-9\-\p{L}\p{Zs}\p{Lu}\p{Ll}\']{1,40}$"))
                args.IsValid = true;
        }
        protected void LastNameValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;

            if (Regex.IsMatch(LastNameTextBox.Text, @"^[0-9\-\p{L}\p{Zs}\p{Lu}\p{Ll}\']{1,40}$"))
                args.IsValid = true;
        }
    }
}
